
var chooseLedgeBaseNo = [];
//关联知识库弹框开启
function setChooseKnowledgeBase(){
    $.ajax({
        url : '/api/inter/knowledgeHitTest/getKnowledgeBase',
        data : {},
        type : 'POST',
        async : false,
        success : function(result) {
            if(result.code=='SUCCESS'){
                chooseLedgeBaseNo = chooseLedgeBaseList;
                var eKnowKnowledgeBase = result.eKnowKnowledgeBase;
                var htmlStr = '<div class="layui-bg-gray" style="padding: 16px;max-height:calc(100vh - 90px);overflow: auto;"><div class="layui-row layui-col-space15">';
                if(eKnowKnowledgeBase!=null && eKnowKnowledgeBase.length>0){
                    for(var i=0;i<eKnowKnowledgeBase.length;i++){
                        var isChooseed = false;
                        var id = eKnowKnowledgeBase[i].id;
                        if(chooseLedgeBaseNo!=null && chooseLedgeBaseNo.length>0){
                            for(var j=0;j<chooseLedgeBaseNo.length;j++){
                                if(chooseLedgeBaseNo[j].id==id)isChooseed=true;
                            }
                        }
                        htmlStr+='<div class="layui-col-md6" style="cursor: pointer;" onclick="chooseLedgeBaseByOpenPage(\''+id+'\',\''+eKnowKnowledgeBase[i].knowledgeBaseName+'\');">'+
                            '<div id="chooseView'+id+'" class="layui-card   choose-view-none '+(isChooseed?'choose-view':'')+'">'+
                            '<div class="layui-card-header" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;" title="'+eKnowKnowledgeBase[i].knowledgeBaseName+'">' +eKnowKnowledgeBase[i].knowledgeBaseName+
                            '<div style="float: right;" id="checkbox'+id+'" '+(isChooseed?'':'hidden')+'><i class="layui-icon layui-icon-ok" style="color: #4BB2FF;font-weight: bold;"></i></div>'+
                            '</div>'+
                            '<div class="layui-card-body" style="height: 100px;"><span class="list-span" title="'+eKnowKnowledgeBase[i].knowledgeBaseDescribe+'">' +eKnowKnowledgeBase[i].knowledgeBaseDescribe+'</span></div></div></div>';
                    }
                }
                htmlStr+='</div>'+
                    '<div class="bottom-view-open">'+
                    '<div class="layui-btn-container add-btn-view-open">'+
                    '<button type="button" class="layui-btn layui-bg-blue add-btn-open" onclick="yesChooseLedgeBaseByOpenPage();">确定</button> '+
                    '</div>'+
                    '</div>';
                layer.open({
                    title:'关联知识库',
                    type: 1,
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    area: ['500px', '100%'],
                    shade: 0.5,
                    shadeClose: true,
                    closeBtn:0,
                    id: 'ID-demo-layer-ledgeBase',
                    content:htmlStr
                })
            }else{
                layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
            }
        }
    });
}

//弹框选择知识库
function chooseLedgeBaseByOpenPage(id,name){
    var isChooseed = false;
    var newChooseList = [];
    if(chooseLedgeBaseNo!=null && chooseLedgeBaseNo.length>0){
        for(var j=0;j<chooseLedgeBaseNo.length;j++){
            if(chooseLedgeBaseNo[j].id==id){
                isChooseed=true;
            }else {
                newChooseList.push({id:chooseLedgeBaseNo[j].id,name:chooseLedgeBaseNo[j].name});
            }
        }
    }
    chooseLedgeBaseNo = newChooseList;
    if(isChooseed){
        $('#chooseView'+id).removeClass('choose-view');
        $('#checkbox'+id).hide();
    }else {
        chooseLedgeBaseNo.push({id:id,name:name});
        $('#chooseView'+id).addClass('choose-view');
        $('#checkbox'+id).show();
    }
}

//确认
function yesChooseLedgeBaseByOpenPage() {
    layer.closeAll();
    chooseLedgeBaseList = chooseLedgeBaseNo;
    resetChooseLedgeBaseSet();
}

//重新设置知识库信息
function resetChooseLedgeBaseSet(){
    var htmlStr = '';
    if(chooseLedgeBaseList!=null && chooseLedgeBaseList.length>0){
        for(var j=0;j<chooseLedgeBaseList.length;j++){
            htmlStr+='<button type="button" class="layui-btn layui-btn-xs"  style="margin-left:0px;margin-right: 10px;" onclick="delChooseLedge(\''+chooseLedgeBaseList[j].id+'\')">'+chooseLedgeBaseList[j].name+' <i class="layui-icon layui-icon-close layui-font-12"></i></button>';

        }
    }
    $('#chooseLedgeBtn').html(htmlStr);
}

//删除选择的AI模型
function delChooseLedge(id){
    var newChooseList = [];
    if(chooseLedgeBaseList!=null && chooseLedgeBaseList.length>0){
        for(var j=0;j<chooseLedgeBaseList.length;j++){
            if(chooseLedgeBaseList[j].id!=id){
                newChooseList.push({id:chooseLedgeBaseList[j].id,name:chooseLedgeBaseList[j].name});
            }
        }
    }
    chooseLedgeBaseList = newChooseList;
    resetChooseLedgeBaseSet();
}




